package test;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 调用python服务
 * @author: huangzhian
 * @date: 2019\4\25 0025
 */
public class PythonService {

    public static String pythonPath = "D:\\work\\pythonWorkspace\\TestThread.py";
    public static String pythonEevPath = "python";

    /**
     * 执行python脚本
     * @param params
     *      第一个参数：python本地完整路径 如" C:\\test.py"
     *      后面参数：所调用的python函数的参数，比如报告的阿里云地址
     *
     */
    public String runScript(String[] params) throws IOException {
        Process proc = Runtime.getRuntime().exec(params);
        return resultHandler(proc);
    }

    /**
     * 执行python脚本
     * @param script 脚本 如"python C:\\test.py"
     *
     */
    public String runScript(String script) throws IOException {
        Process proc = Runtime.getRuntime().exec(script);
        return resultHandler(proc);
    }

    /**
     * python脚本执行结果处理
     * @param proc
     * @return
     * @throws IOException
     */
    private String resultHandler(Process proc) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        StringBuilder result = new StringBuilder();
        try {
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            if ("error".equals(result.toString())) {
                throw new IOException("python脚本异常");
            }
            return result.toString();
        } catch (IOException e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (proc != null) {
                proc.destroy();
            }
        }
    }

    public static void main(String[] args) throws Exception{
        PythonService pythonService = new PythonService();
        String[] allParams = {pythonEevPath, pythonPath, "1"};
        String[] allParams_2 = {pythonEevPath, pythonPath, "2"};
        String[] allParams_3 = {pythonEevPath, pythonPath, "3"};
        String[] allParams_4 = {pythonEevPath, pythonPath, "4"};
        String[] allParams_5 = {pythonEevPath, pythonPath, "5"};
        String[] allParams_6 = {pythonEevPath, pythonPath, "6"};
        String[] allParams_7 = {pythonEevPath, pythonPath, "7"};

        List<String[]> list = new ArrayList<>();
        list.add(allParams);
        list.add(allParams_2);
        list.add(allParams_3);
        list.add(allParams_4);
        list.add(allParams_5);
        list.add(allParams_6);
        list.add(allParams_7);

        for (String[] allParam : list) {
            ThreadPoolUtil.riskArchiveTask.submit(() -> {
                try {
                    String result = pythonService.runScript(allParam);
                    System.out.println(new Date() + " " +result);
                }catch (Exception e){
                    System.out.println(e);
                }
            });
        }
        ThreadPoolUtil.riskArchiveTask.shutdown();

    }

}
